package com.base.myproject.client.busi.system;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.selection.BaseCheckBoxSelectionModel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

public class Bcode extends BaseJDBCPanel {
	SelectionListener<ButtonEvent> listener ;
	BaseStore<ModelData> bcodestore;
	BaseGrid<ModelData> grid;
	public Bcode()
	{
		init();
	}
	public void init() {

		//super.init();
		initlistener();
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	this.add(initWeiget(),centerData);
	}

	public ContentPanel initWeiget() {

		BaseCheckBoxSelectionModel cbs = new BaseCheckBoxSelectionModel();

		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig bcode = new ColumnConfig("bcode", "人员编码", 100);
		TextField<String> bcodetext = new TextField<String>();
		bcode.setEditor(new CellEditor(bcodetext));
		columns.add(bcode);

		ColumnConfig bname = new ColumnConfig("bname", "人员名", 100);
		TextField<String> bnametext = new TextField<String>();
		
		bname.setEditor(new CellEditor(bnametext));
		columns.add(bname);

		ColumnModel cm = new ColumnModel(columns);

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.addField("bcode");
		type.addField("bname");

		StoreInfo info = new StoreInfo();
		info.setTablename("bcode");
		info.setSql("select * from bcode");
		info.setKeyname(new String[] { "bcode"});
		 bcodestore = new BaseStore<ModelData>(type, info);
		
		
		 grid = new BaseGrid<ModelData>(bcodestore, cm);
		grid.setAutoWidth(true);
		grid.setAutoHeight(true);
		ContentPanel panel = new ContentPanel();
		panel.setLayout(new FitLayout());
		panel.add(grid);
		cbs.setSelectionMode(SelectionMode.MULTI);
		grid.setSelectionModel(cbs);
		
		// 所有组织完成后，要注册store
		setMaincomponent(grid);
		registerMainStore(bcodestore);
		this.registerStore(bcodestore);
		Button b = new Button("test");
		panel.add(b);
		b.addSelectionListener(listener);
		return panel;
	}
	void initlistener()
	{
	 listener  = new SelectionListener<ButtonEvent>()
		{

			@Override
			public void componentSelected(ButtonEvent ce) {
				
				System.out.println("class--"+bcodestore.getAt(4).getClass());
				//System.out.println("class--2"+grid.getSelectionModel().getSelectedItems().get(0).getClass());
				
				//System.out.println("class--"+bcodestore.getAt(0).getClass());
				System.out.println("class3--"+((BusiObject)bcodestore.getAt(4)).getBusistatus());
				System.out.println("class4--"+((BusiObject)bcodestore.getAt(4)).getStoreinfo().getTablename());
				System.out.println("class5--"+((BusiObject)bcodestore.getAt(4)).getStoreinfo().getKeyname().length);
				System.out.println("class6--"+((BusiObject)bcodestore.getAt(4)).getKeyOldValue("bcode"));
				System.out.println("class7--"+((BusiObject)bcodestore.getAt(1)).getKeyOldValue("bcode"));
			
			
			}
			
		};
	}
}
